<?php
$book 	=	array();
if(isset($_GET['id'])){
	$id	=	intval($_GET['id']);
	$q	=	mysql_query("SELECT * FROM sach WHERE `id`=".$id);
	$book	=	mysql_fetch_array($q);
}
?>
<div class="mainright">
	<div class="tableout">
	
			<div class="title1">
	            <div class="column" style="width:100%;">Add new tour:</div>
	        </div>
	
			<div class="editcate_ct">
			
			    <form method="post" action="" enctype="multipart/form-data" id="news_form">
			    
			    <div id="div_message"></div>
			    
				<div class="boxadd">
			    	<ul class="linect">
			        	<span>Select content type : </span>
			            <select name="cat" style="width: 200px; font-size: 12px; font-weight: bold;" id="content_type">
			            	<?php 
			            	$q	=	mysql_query("SELECT * FROM phanloai");
			            	while($row	=	mysql_fetch_array($q)):
			            	?>
			            	<option <?php echo ($book['ma_danh_muc'] == $row['pl_id'] ? 'selected="selected"' : '');?> value="<?php echo $row['pl_id'];?>"><?php echo $row['pl_title'];?></option>
			            	<?php endwhile;?>
			                
			            </select>
			        
			        <li>
			        	<span class="left"><b>Tên sách:</b></span>
			        	<span class="right"><input name="name" value="<?php echo (isset($_POST['name']) ? $_POST['name'] : ($book ? $book['ten_sach'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Tác giả:</b></span>
			        	<span class="right"><input name="author" value="<?php echo (isset($_POST['author']) ? $_POST['author'] : ($book ? $book['tac_gia'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Nhà xuất bản:</b></span>
			        	<span class="right"><input name="publisher" value="<?php echo (isset($_POST['publisher']) ? $_POST['publisher'] : ($book ? $book['nha_xb'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Giá bìa:</b></span>
			        	<span class="right"><input name="price" value="<?php echo (isset($_POST['price']) ? $_POST['price'] : ($book ? $book['gia_bia'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Số trang:</b></span>
			        	<span class="right"><input name="num" value="<?php echo (isset($_POST['num']) ? $_POST['num'] : ($book ? $book['so_trang'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Trọng lượng:</b></span>
			        	<span class="right"><input name="weight" value="<?php echo (isset($_POST['weight']) ? $_POST['weight'] : ($book ? $book['trong_luong'] : ''));?>" style="width: 40%; margin: 0pt;" type="text"></span>
			        </li>
			        <li>
			        	<span class="left"><b>Ảnh:</b></span>
			            <span class="right"><input name="img" value="" type="file">(210 x 120)</span>
			        </li>
			        <li>
			        	<span class="left"><b>Thông tin: </b></span>
			            <span class="right">
			            <?php 
			            require_once('fckeditor/fckeditor.php');
						$FCKeditor = new FCKeditor('content');
						$FCKeditor->BasePath = 'fckeditor/';
						$FCKeditor->Value = (isset($_POST['name']) ? $_POST['name'] : ($book ? $book['gioi_thieu'] : ''));
						//$FCKeditor->Config["CustomConfigurationsPath"] = "js/fckconfig.js";
						//$FCKeditor->ToolbarSet = 'Custom';
						$FCKeditor->Width = '80%';
						$FCKeditor->Height = '300px';	
						$FCKeditor->Create();
						
			            ?>
			            </span>
			        </li>
			
			        </ul>
			    </div>
			    <div class="btarticle">
			    	<input onclick="$('#light_adct, #fade_adct').hide();" value="Cancel" class="btn" type="button">
			        <input name="" value="Save &amp; Continute" class="btn" type="submit">
			    </div>
			    
			    </form>
			    
			</div>
	</div>

</div>

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST'){
	$img_path	=	'';
	if($_FILES['img']){
		$dir_root	=	$_SERVER['DOCUMENT_ROOT'];
		$upload_dir = $dir_root.'/'.$CONFIG['sub_dir'].$CONFIG['upload_dir'];
		
	    create_dir($upload_dir);
	    $new_path = $upload_dir.$_FILES["img"]["name"];
	    $info	=	pathinfo($new_path);
	    $new_name	=	md5(date('ymdhis')).'.'.$info['extension'];
	    $img_path	=	$CONFIG['upload_dir'].$new_name;
	    $new_path = $upload_dir.$new_name;
		move_uploaded_file($_FILES["img"]['tmp_name'],$new_path);
	}
	else{
		if($book)$img_path	=	$book['anh'];
	}
	if(isset($_GET['id']) && intval($_GET['id']) > 0){
		mysql_query("UPDATE sach SET `ma_danh_muc`=".intval($_POST['cat']).",`ten_sach`='".mysql_escape_string($_POST['name'])."',`anh`='".$img_path."',`gioi_thieu`='".mysql_escape_string($_POST['content'])."',`tac_gia`='".$_POST['author']."',`nha_xb`='".$_POST['publisher']."',`gia_bia`='".$_POST['price']."',`so_trang`='".$_POST['num']."',`trong_luong`='".$_POST['weight']."' WHERE `id`=".intval($_GET['id']));
	}
	else{
		mysql_query("INSERT INTO sach(`ma_danh_muc`,`ten_sach`,`gioi_thieu`,`tac_gia`,`nha_xb`,`gia_bia`,`so_trang`,`trong_luong`,`ngay_tao`)VALUES(".intval($_POST['cat']).",'".mysql_escape_string($_POST['name'])."','".mysql_escape_string($_POST['content'])."','".$_POST['author']."','".$_POST['publisher']."','".$_POST['price']."','".$_POST['num']."','".$_POST['weight']."','".date('Y-m-d H:i:s')."')");
	}
	echo "<script>location.href='index.php?act=list_book';</script>";
}
?>

